var civResTimer = 0;
function addResource(resource, addNumber) {

	if (totalRes - addNumber < 0) {
		jalert("No more free people left", 2);
		return;
	}
	totalRes = totalRes - addNumber;

	var resType = -1;
	switch (resource) {
	case "addfood":
		resType = 0;
		break;
	case "addwater":
		resType = 1;
		break;
	case "addhay":
		resType = 2;
		break;
	case "addwood":
		resType = 3;
		break;
	case "addsilver":
		resType = 4;
		break;
	case "addgold":
		resType = 5;
		break;
	default:
		resType = 6;
		return;
		break;
	}

	resources[resType][0] += addNumber;
	isResDirty[resType] = true;
	syncResControls();
	// setInterval(modCivAsync, 100000);
	clearTimeout(civResTimer);
	civResTimer = setTimeout(modCivAsync, 5000);
}

function remResource(resource, addNumber) {

	var resType = -1;
	switch (resource) {
	case "remfood":
		resType = 0;
		break;
	case "remwater":
		resType = 1;
		break;
	case "remhay":
		resType = 2;
		break;
	case "remwood":
		resType = 3;
		break;
	case "remsilver":
		resType = 4;
		break;
	case "remgold":
		resType = 5;
		break;
	default:
		jalert("dont know " + resource, 1);
		break;
	}
	if ((resources[resType][0] - addNumber) < 0) {
		jalert("No more resource left to be removed!", 3);
		return;
	}

	resources[resType][0] -= addNumber;
	totalRes = totalRes + addNumber;
	isResDirty[resType] = true;
	syncResControls();
	// setInterval(modCivAsync, 100000);
	clearTimeout(civResTimer);
	civResTimer = setTimeout(modCivAsync, 5000);
}

function syncResData() {
	// var total = 0;
	// for ( var x = 0; x < 6; x++) {
	// total += resources[x][1];
	// }

	for ( var x = 0; x < MAX_CIV_RES; x++) {
		// var perc = (resources[x][1] * 100) / total;
		$('#resperc' + x).html(costhumb[x] + "" + resources[x][1]);// + "("+
		// parseInt(perc)+"%)");
	}

}

function syncResControls() {

	$('#resfood').html(resources[0][0] + " civilians");
	$('#reswater').html(resources[1][0] + " civilians");
	$('#reshay').html(resources[2][0] + " civilians");
	$('#reswood').html(resources[3][0] + " civilians");
	$('#ressilver').html(resources[4][0] + " civilians");
	$('#resgold').html(resources[5][0] + " civilians");
	// $('#resTotal').html(totalRes + " ");
	changeIdleResource(totalRes);
}

function addMilResource(resource, addNumber) {
	var restType = -1;

	switch (resource) {
	case "createArmy":
		restType = 0;
		break;
	case "createTank":
		restType = 1;
		break;
	case "createAir":
		restType = 2;
		break;
	case "createNavy":
		restType = 3;

		break;
	case "createSpy":
		restType = 4;

		break;
	case "createSubs":
		restType = 5;

		break;

	default:
		jalert("dont know " + resource, 1);
		return;
		break;
	}

	if (milcost[restType][0] > resources[0][1]) {
		jalert("Insufficient food!", 1);
		return;
	}
	if (milcost[restType][1] > resources[1][1]) {
		jalert("Insufficient Oil!", 1);
		return;
	}
	if (milcost[restType][2] > resources[2][1]) {
		jalert("Insufficient wood!", 1);
		return;
	}
	if (milcost[restType][3] > resources[3][1]) {
		jalert("Insufficient water!", 1);
		return;
	}
	if (milcost[restType][4] > resources[4][1]) {
		jalert("Insufficient silver!", 1);
		return;
	}
	if (milcost[restType][5] > resources[5][1]) {
		jalert("Insufficient gold!", 1);
		return;
	}
	for ( var x = 0; x < 6; x++) {
		resources[x][1] = resources[x][1] - milcost[restType][x];
	}
	mresources[restType] += addNumber;
	addMil(restType, mresources[restType]);
	syncMilControls();
	syncResData();
}

function modCivAsync() {
	for (i = 0; i < 6; i++) {
		if (isResDirty[i] == true) {
			addCivilian(i, resources[i][0]);
		}
	}

}

function addCivilian(resType, totalCivs) {

	$.getJSON("addcivilian/add", {
		civType : resType,
		civNum : totalCivs
	}, function(rtnmsg) {
		isResDirty[resType] = false;
		syncResControls();
	});
}

function syncMilControls() {

	$('#resArmy').html(mresources[0] + " Army");
	$('#resTank').html(mresources[1] + " Tanks");
	$('#resAir').html(mresources[2] + " Air");
	$('#resNavy').html(mresources[3] + " Navy");
	$('#resSpies').html(mresources[4] + " Spies");
	$('#resSubs').html(mresources[5] + " Subs");
}

function addMil(resType, totalMils) {

	$.getJSON("addmilitary/add", {
		milType : resType,
		milNum : totalMils
	}, function(rtnmsg) {
		syncResControls();
	});
}

function parseResourceMessage(availability) {
	var food, water, oil, wood, silver, gold, dollar, xp, karma;
	food = parseInt(availability.res0);
	water = parseInt(availability.res1);
	oil = parseInt(availability.res2);
	wood = parseInt(availability.res3);
	silver = parseInt(availability.res4);
	gold = parseInt(availability.res5);
	dollar = parseInt(availability.res6);
	xp = parseInt(availability.res7);
	karma = parseInt(availability.res8);

	if (isNaN(food) || isNaN(water) || isNaN(oil) || isNaN(wood)
			|| isNaN(silver) || isNaN(gold)) {
		// uh oh! FIXME. Try to update again?
		jalert(
				"Problem trying to update from server. Will retry in few minutes",
				2)
		return;
	}
	resources[0][1] = food;
	resources[1][1] = water;
	resources[2][1] = oil;
	resources[3][1] = wood;
	resources[4][1] = silver;
	resources[5][1] = gold;
	resources[6][1] = dollar;
	resources[7][1] = xp;
	resources[8][1] = karma;
	syncResData();
}

function syncUpdateServer() {
	$.getJSON("getallcivs/getallcivresources", function(availability) {
		if (null == availability) {
			jalert("Unable to contact server! Are you online?", 2)
			return;
		}
		parseResourceMessage(availability);
	});
}

function checkNotice() {
	$.getJSON("comet/pull", function(allnotices) {
		if (null == allnotices) {
			jalert("Unable to contact server! Are you online?", 2)
			return;
		}

		if (allnotices.notices) {
			updatesMessages(allnotices.notices);
		}
		

	});
}

function updatesMessages(msgs) {

	var i = 0;
	for (n in msgs) {
		var obj = msgs[i++]
		if(null!=obj && obj!=undefined)
		$("#messages").append("<li class='ui-widget-content'>" + obj.msgBody+ " <small class='msgdate'>(" + obj.sendDate+ ")</small> </li>");
	}
	if (i > 0) {
		$("#messages").selectable();
		$("#msgtext").html(i);
		$("#nomessages").html("")
	}
	

}

function syncServer() {

	$.getJSON("getallcivswoupdate/getallcivresources", function(availability) {

		if (null == availability) {
			jalert("Unable to contact server! Are you online?", 2)
			return;
		}
		parseResourceMessage(availability);
	});
}

function cometPull() {
	$.getJSON("comet/pull", function(availability) {
		if (null == availability) {
			jalert("Unable to contact server! Are you online?", 2)
			return;
		}
		var statuscode = availability.scode;

		switch (statuscode) {
		case 1:
			// update civ resources
			break;
		case 2:
			// update mil resources
			break;
		case 3:
			// update notification
			break;
		case 4:
			// update territories
			break;
		case 5:
			// update war locations
			break;
		default:
			// dont know who i am. who you are. where you are sing along dude..
		}
	});
}

function syncMilServer() {
	$.getJSON("getallmilres/getall", function(availability) {

		if (null == availability) {
			jalert("Unable to contact server! Are you online?", 2)
			return;
		}
		var army, tank, air, ship, sub, spy;

		army = parseInt(availability.res0);
		tank = parseInt(availability.res1);
		air = parseInt(availability.res2);
		ship = parseInt(availability.res3);
		sub = parseInt(availability.res4);
		spy = parseInt(availability.res5);

		if (isNaN(army) || isNaN(tank) || isNaN(air) || isNaN(ship)
				|| isNaN(sub) || isNaN(spy)) {
			// uh oh! FIXME. Try to update again?
			return;
		}
		mresources[0] = army;
		mresources[1] = tank;
		mresources[2] = air;
		mresources[3] = ship;
		mresources[4] = sub;
		mresources[5] = spy;
		syncMilControls();
	});
}

function checkForInvalid(rtnmsg) {
	if (null == rtnmsg) {
		return 0;
	}
	if (rtnmsg.status != null) {

	}
	return 200;
}
